//
//  SelectAccountController.m
//  ATMCash


#import "SelectAccountController.h"
#import "SelectAccountTableCell.h"
#import "BankVertifyController.h"

#define kTabCell  @"kTabCell"

#define kFootViewHeight (kHeight_BottomSafe + 90 + 90)

@interface SelectAccountController ()

@property (nonatomic) NSMutableArray *vanityAry;

@property (nonatomic) NSString *bankIdStr;

@end

@implementation SelectAccountController

- (void)viewDidLoad {
    self.isTableViewStyleGrouped = YES;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Select Account";
    
    self.view.backgroundColor = self.tableView.backgroundColor = RGB(241, 243, 242);
    
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kFootViewHeight);
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"icon_add"] target:self action:@selector(clickRightItem)];
    
    [self initFootView];
    
    [self loadData];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 7.5)];
    
    [self.tableView registerClass:[SelectAccountTableCell class] forCellReuseIdentifier:kTabCell];
}

- (void)loadData{
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    [BaseDataEngine apiForPath:kBankCardListUrl method:kPostMethod parameter:nil onCompleteSuccess:^(NSDictionary *responseDic) {
        [CreaditUtilsHelper hideLoadMessageBox];
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }
        [self.data removeAllObjects];
        
        NSDictionary *dic = responseDic[@"recollecting"];
        NSArray *vanityAry = dic[@"vanity"];
      
        self.vanityAry = [NSMutableArray arrayWithArray:vanityAry];
        
        self.data = [SelectAccountModel mj_objectArrayWithKeyValuesArray:vanityAry];
        [self.tableView reloadData];
        
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SelectAccountModel *mm = self.data[section];
    return mm.victory.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectAccountTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kTabCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SelectAccountModel *mm = self.data[indexPath.section];
    
    if (mm.victory.count > indexPath.row) {
        AccountVictoryModel *contentModel = mm.victory[indexPath.row];
        cell.giggling = esString(mm.giggling);
        cell.model = contentModel;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];


    SelectAccountTableCell *selCell  = [tableView cellForRowAtIndexPath:indexPath];
    
    SelectAccountModel *topModel = self.data[indexPath.section];
    
    AccountVictoryModel *mm = selCell.model;
    
    [self clearAllSelect];
    
    NSMutableDictionary *muDic = [[NSMutableDictionary alloc]init];
    [muDic setObject:esString(topModel.changed) forKey:@"changed"];
    [muDic setObject:esString(topModel.giggling) forKey:@"giggling"];
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:topModel.victory];
    
    for (int i=0; i< array.count; i++) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        NSMutableDictionary *awfulDic = [NSMutableDictionary dictionary];
        
        AccountVictoryModel *mm = [AccountVictoryModel mj_objectWithKeyValues:array[i]];
        
        [dic setObject:esString(mm.sorry) forKey:@"sorry"];
        [dic setObject:esString(mm.leaning) forKey:@"leaning"];
        [dic setObject:esString(mm.spoon) forKey:@"spoon"];
        [dic setObject:esString(mm.please) forKey:@"please"];
        
        [awfulDic setValue:esString(mm.awfully.sole) forKey:@"sole"];
        [awfulDic setValue:esString(mm.awfully.operas) forKey:@"operas"];
        [awfulDic setValue:esString(mm.awfully.wagner) forKey:@"wagner"];
        
        [dic setValue:awfulDic forKey:@"awfully"];
        
        if (i == indexPath.row) {
            [dic setObject:@YES forKey:@"isSelected"];
        }
        else {
            [dic setObject:@NO forKey:@"isSelected"];
        }
        [array replaceObjectAtIndex:i withObject:dic];
        
        [muDic setObject:array forKey:@"victory"];
    }
    
    [self.vanityAry replaceObjectAtIndex:indexPath.section withObject:muDic];
    
    self.data = [SelectAccountModel mj_objectArrayWithKeyValuesArray:self.vanityAry];
    

    [self.tableView reloadData];
    
    self.bankIdStr = esString(mm.leaning);
    DLog(@"====%@===%@",topModel.changed,mm.leaning);
    
    
}

- (void)clearAllSelect{
    
    NSMutableArray *array1 = [NSMutableArray array];
    
    for (int i =0;i<self.vanityAry.count; i++) {
        
        NSMutableArray *array2 = [NSMutableArray array];
        
        NSArray *contentAry = self.vanityAry[i][@"victory"];
        
        NSMutableDictionary *muDic = [[NSMutableDictionary alloc]init];
        
        SelectAccountModel *mde = [SelectAccountModel mj_objectWithKeyValues:self.vanityAry[i]];
        
        [muDic setObject:esString(mde.changed) forKey:@"changed"];
        [muDic setObject:esString(mde.giggling) forKey:@"giggling"];
        

        for (int j=0; j<contentAry.count; j++) {

            NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
            NSMutableDictionary * awfulDic = [[NSMutableDictionary alloc]init];
            
            AccountVictoryModel *model = [AccountVictoryModel mj_objectWithKeyValues:contentAry[j]];
            [dic setObject:esString(model.sorry) forKey:@"sorry"];
            [dic setObject:esString(model.leaning) forKey:@"leaning"];
            [dic setObject:esString(model.spoon) forKey:@"spoon"];
            [dic setObject:esString(model.please) forKey:@"please"];
            
            [awfulDic setValue:esString(model.awfully.sole) forKey:@"sole"];
            [awfulDic setValue:esString(model.awfully.operas) forKey:@"operas"];
            [awfulDic setValue:esString(model.awfully.wagner) forKey:@"wagner"];
            
            [dic setValue:awfulDic forKey:@"awfully"];
            
            [dic setObject:@NO forKey:@"isSelected"];
            
            [array2 addObject:dic];
        }
        
        [muDic setObject:array2 forKey:@"victory"];
        
        [array1 addObject:muDic];
        
    }

    self.vanityAry = [NSMutableArray arrayWithArray:array1];
    self.data = [SelectAccountModel mj_objectArrayWithKeyValuesArray:array1];
    
    [self.tableView reloadData];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.data.count == 0) {
        return 0.000f;
    }
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.data.count == 0) {
        return nil;
    }
    
    SelectAccountModel *mm  = self.data[section];
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    sectionView.backgroundColor = [UIColor clearColor];
    
    NSString *nameStr = esString(mm.changed);
    
    UIImageView *leftV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 20, 20)];
    leftV.image = [UIImage imageNamed:@"icon_leftTips"];
    [sectionView addSubview:leftV];
    
    CGSize size = [StringUtil textSizeWithText:nameStr font:[UIFont fontWithName:kFutura size:15] numberOfLines:1 lineSpacing:0 constrainedWidth:200];
    UILabel *lab = [Utility createLabel:[UIFont fontWithName:kFutura size:15] color:RGB(2, 45, 6)];
    lab.text = nameStr;
    lab.frame = CGRectMake(leftV.right + 2, 0, size.width + 5, 40);
    [sectionView addSubview:lab];
    
    UIImageView *rightV = [[UIImageView alloc] initWithFrame:CGRectMake(lab.right + 2, 10, 20, 20)];
    rightV.image = [UIImage imageNamed:@"icon_rightTips"];
    [sectionView addSubview:rightV];
    
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc] init];
    vv.backgroundColor = [UIColor clearColor];
    return vv;
}


- (void)initFootView{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kFootViewHeight - kNavigationBarHeight, kScreenWidth, kFootViewHeight)];
    footView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:footView];
    
    UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, kScreenWidth - 50, 50)];
    addBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    [addBtn setTitleColor:RGB(79, 180, 131) forState:UIControlStateNormal];
    [addBtn setTitleColor:RGB(79, 180, 131) forState:UIControlStateSelected];
    [addBtn setTitleColor:RGB(79, 180, 131) forState:UIControlStateHighlighted];
    [addBtn setTitle:@" Add Other Payment Methods" forState:UIControlStateNormal];
    [addBtn setTitle:@" Add Other Payment Methods" forState:UIControlStateSelected];
    [addBtn setTitle:@" Add Other Payment Methods" forState:UIControlStateHighlighted];
    [addBtn setImage:[UIImage imageNamed:@"icon_add"] forState:UIControlStateNormal];
    [addBtn setImage:[UIImage imageNamed:@"icon_add"] forState:UIControlStateSelected];
    [addBtn setImage:[UIImage imageNamed:@"icon_add"] forState:UIControlStateHighlighted];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"icon_accountBtn2"] forState:UIControlStateNormal];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"icon_accountBtn2"] forState:UIControlStateSelected];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"icon_accountBtn2"] forState:UIControlStateHighlighted];
    [addBtn addTarget:self action:@selector(clickRightItem) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:addBtn];
    
    UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(0, footView.size.height - kHeight_BottomSafe - 90, kScreenWidth, 90 + kHeight_BottomSafe)];
    bottomV.backgroundColor = [UIColor whiteColor];
    [footView addSubview:bottomV];
    
    UIButton *bottomBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, kScreenWidth- 50, 50)];
    bottomBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    [bottomBtn setTitleColor:RGB(231, 238, 194) forState:UIControlStateNormal];
    [bottomBtn setTitleColor:RGB(231, 238, 194) forState:UIControlStateSelected];
    [bottomBtn setTitleColor:RGB(231, 238, 194) forState:UIControlStateHighlighted];
    [bottomBtn setTitle:@"Submit Loan" forState:UIControlStateNormal];
    [bottomBtn setTitle:@"Submit Loan" forState:UIControlStateSelected];
    [bottomBtn setTitle:@"Submit Loan" forState:UIControlStateHighlighted];
    [bottomBtn setBackgroundImage:[UIImage imageNamed:@"icon_accountBtn"] forState:UIControlStateNormal];
    [bottomBtn setBackgroundImage:[UIImage imageNamed:@"icon_accountBtn"] forState:UIControlStateSelected];
    [bottomBtn setBackgroundImage:[UIImage imageNamed:@"icon_accountBtn"] forState:UIControlStateHighlighted];
    [bottomBtn addTarget:self action:@selector(clickSubmit) forControlEvents:UIControlEventTouchUpInside];
    [bottomV addSubview:bottomBtn];
    
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)clickRightItem{
    BankVertifyController *vc = [[BankVertifyController alloc] init];
    vc.idStr = esString(self.orderIdStr);
    vc.orderNoStr = esString(self.orderNoStr);
    vc.isFromH5ChangeBankCard = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)clickSubmit{
    if (PMStringIsEmpty(self.bankIdStr)) {
        [CreaditUtilsHelper boxShowWithMessage:@"Please Select Account"];
        return;
    }

    [self changeBankUrl:self.bankIdStr];

}

#pragma mark ----changeBankUrl
- (void)changeBankUrl:(NSString *)bankIdStr{
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    NSMutableDictionary *muDic = @{
        @"leaning":esString(bankIdStr),
        @"commissariat":esString(self.orderNoStr),
    }.mutableCopy;
    [BaseDataEngine apiForPath:kChangeBankCardUrl method:kPostMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        [CreaditUtilsHelper hideLoadMessageBox];
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }
        
        NSDictionary *dic = responseDic[@"recollecting"];
        
        [[CreditCore globleCore] jumpToPage:esString(dic[@"stunted"])];
        
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:YES];
}


@end
